package cn.cds.withyou.admin.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.cds.withyou.admin.R;
import cn.cds.withyou.lib.model.FeedbackDto;

/**
 * <p> Description: </p>
 * <p/>
 * <p> Copyright: Copyright (c) 2016 </p>
 *
 * @author: Json.Lee
 * @Version: 1.0
 * @CreateTime: 7/5/2016 23:24
 */
public class FeedbackAdapter extends RecyclerView.Adapter<FeedbackAdapter.FeedbackHolder> {

    private Context mContext;
    private List<FeedbackDto> mData;

    private final static SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    static {
        FORMAT.setTimeZone(TimeZone.getDefault());
    }

    public FeedbackAdapter (Context context, List<FeedbackDto> data) {
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public FeedbackHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.activity_feedback_item, parent, false);
        return new FeedbackHolder(v);
    }

    @Override
    public void onBindViewHolder(FeedbackHolder holder, int position) {
        FeedbackDto dto = mData.get(position);
        holder.nameTv.setText(dto.getName());
        holder.phoneTv.setText(dto.getPhone());
        holder.contentTv.setText(dto.getContent());
        holder.timeTv.setText(FORMAT.format(new Date(dto.getTime())));
    }

    @Override
    public int getItemCount() {
        return null == mData ? 0 : mData.size();
    }

    public static class FeedbackHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.name_tv)
        TextView nameTv;
        @Bind(R.id.phone_tv)
        TextView phoneTv;
        @Bind(R.id.content_tv)
        TextView contentTv;
        @Bind(R.id.time_tv)
        TextView timeTv;

        public FeedbackHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);
        }
    }
}
